﻿var vm = new Vue({
    el: '#app'
    , data: {
        databases: []
        , tables: []
        , tabs: [{
            title: 'Model'
            , name: 'Model'
            , content: ''
            , namespace: 'FSM.Model'
            , className: ''
            , tpl: ''
            , fields: []
            , selected_fields: []
            , path: 'E:\\WorkSpace\\ST系統\\文件管理系统\\FSM\\FSM.Model'
        }, {
            title: 'IDal'
            , name: 'IDal'
            , content: ''
            , fields: []
            , selected_fields: []
            , namespace: 'FSM.IDal'
            , className: ''
            , tpl: ''
            , path: 'E:\\WorkSpace\\ST系統\\文件管理系统\\FSM\\FSM.IDal\\Temp'
        },
        {
            title: 'Dal'
            , name: 'Dal'
            , content: ''
            , fields: []
            , selected_fields: []
            , namespace: 'FSM.Dal'
            , className: ''
            , tpl: ''
            , path: 'E:\\WorkSpace\\ST系統\\文件管理系统\\FSM\\FSM.Dal\\Temp'
        },
        {
            title: 'IBll'
            , name: 'IBll'
            , content: ''
            , fields: []
            , selected_fields: []
            , namespace: 'FSM.IBll'
            , className: ''
            , tpl: ''
            , path: 'E:\\WorkSpace\\ST系統\\文件管理系统\\FSM\\FSM.IBll\\Temp'
        },
        {
            title: 'Bll'
            , name: 'Bll'
            , content: ''
            , fields: []
            , selected_fields: []
            , namespace: 'FSM.Bll'
            , className: ''
            , tpl: ''
            , path: 'E:\\WorkSpace\\ST系統\\文件管理系统\\FSM\\FSM.Bll\\Temp'
        }, {
            title: 'Controller'
            , name: 'Controller'
            , content: ''
            , fields: []
            , selected_fields: []
            , namespace: 'FSM.Web.Controllers'
            , className: ''
            , tpl: ''
            , path: 'E:\\WorkSpace\\ST系統\\文件管理系统\\FSM\FSM.Web\\Controllers\\Projects'
        }, {
            title: 'Js'
            , name: 'Js'
            , content: ''
            , fields: []
            , selected_fields: []
            , namespace: ''
            , className: ''
            , tpl: ''
            , path: 'E:\\WorkSpace\\ST系統\\文件管理系统\\FSM\FSM.Web\\wwwroot\\module\\projects'
        }, {
            title: 'View'
            , name: 'View'
            , content: ''
            , fields: []
            , selected_fields: []
            , namespace: ''
            , className: ''
            , tpl: ''
            , path: 'E:\\WorkSpace\\ST系統\\文件管理系统\\FSM\\FSM.Web\\Views'
        }]
        , selected: {
            database: ''
            , activeTabsName: 'Model'
            , tableName: ''
        }
    }
    , mounted: function () {
        this.getDatabases();
        //this.getTables();
    }
    , watch: {
        tabs: {
            handler(newValue, oldValue) {
                let len = newValue.length;
                for (let i = 0; i < len; i++) {
                    if (newValue[i].namespace != oldValue[i].namespace || newValue[i].className != oldValue[i].namespace) {
                        this.buildModelFile(false);
                        this.buildIDalFile(false);
                        this.buildDalFile(false);
                        this.buildIBllFile(false);
                        this.buildBllFile(false);
                        this.buildControllerFile(false);
                        this.buildJsFile(false);
                        this.buildViewFile(false);
                        break;
                    }
                }
            },
            deep: true
        }
    }
    , methods: {
        getDatabases: function () {
            const me = this;
            axios.post('/CodeMode/ListDatabases').then(response => {
                if (response.data && response.data.success) {
                    me.databases = response.data.data;
                    if (me.databases && me.databases.length > 0) {
                        me.selected.database = me.databases[0].schema_name;
                        me.getTables(me.selected.database);
                    }
                }
                else {
                    if (response.data) {
                        me.$message.error(response.data.message);
                    }
                    else {
                        me.$message.error('抓取数据库信息失败');
                    }
                }
            })
        }
        , getTables: function (selVal) {
            const me = this;
            axios.post('/CodeMode/ListTables/' + selVal, {}).then(function (response) {
                if (response.data && response.data.success) {
                    me.tables = response.data.data
                }
                else {
                    if (response.data) {
                        me.$message.error(response.data.message);
                    }
                    else {
                        me.$message.error('抓取表信息失败');
                    }
                }
            }).catch(function (error) {
                me.$message.error(error);
            });
        }
        , async getFields(table_name) {
            const me = this;
            me.selected.tableName = table_name;
            axios.post('/CodeMode/ListFields/', {
                database: me.selected.database,
                table: table_name
            }).then(function (response) {
                if (response.data && response.data.success) {
                    //me.fields = response.data.data;
                    //console.log(JSON.parseJson(JSON.stringify(response.data.data)));
                    me.tabs[0].fields = JSON.parse(JSON.stringify(response.data.data));
                    me.tabs[3].fields = JSON.parse(JSON.stringify(response.data.data));
                    //me.tabs[5].fields = JSON.parse(JSON.stringify(response.data.data));
                    me.tabs[6].fields = JSON.parse(JSON.stringify(response.data.data));
                    me.tabs[7].fields = JSON.parse(JSON.stringify(response.data.data));

                    me.tabs[0].selected_fields.push.apply(me.tabs[0].selected_fields, me.tabs[0].fields.map(item => item.column_name));
                    me.tabs[6].selected_fields.push.apply(me.tabs[6].selected_fields, me.tabs[6].fields.map(item => item.column_name));
                    me.tabs[7].selected_fields.push.apply(me.tabs[7].selected_fields, me.tabs[7].fields.map(item => item.column_name));
                    setTimeout(async () => {
                        await me.handleCheckedFieldsChange(true);
                    }, 10);
                }
                else {
                    if (response.data) {
                        me.$message.error(response.data.message);
                    }
                    else {
                        me.$message.error('抓取表信息失败');
                    }
                }
            }).catch(function (error) {
                me.$message.error(error);
            })
        }
        , async handleCheckedFieldsChange(is_first) {
            let me = this;
            await me.buildModelFile(is_first);
            await me.buildIDalFile(is_first)
            await me.buildDalFile(is_first);
            await me.buildIBllFile(is_first);
            await me.buildBllFile(is_first);
            await me.buildControllerFile(is_first);
            await me.buildJsFile(is_first);
            await me.buildViewFile(is_first);
        }
        , getKeyFlag(key) {
            return key == "PRI" ? "el-icon-star-on" : "";
        }
        , transTableName() {
            let me = this;
            let arr = me.selected.tableName.split('_');
            arr = arr.map(item => {
                let ret = item[0].toUpperCase() + item.substring(1)
                if (ret == 'Tbl') ret = '';
                return ret;
            });
            return arr.join('');
        }
        , getKeyAndKeyType() {
            let me = this;
            let list = me.tabs[0].fields.find(item => item.column_key == 'PRI');
            if (list) {
                return [list.column_name, list.data_type];
            }
            else {
                return ['键值只能为1个', '无'];
            }
        }
        , async buildModelFile(is_first) {
            let me = this;
            if (!me.tabs[0].tpl) {
                await axios.get('/lib/template/tpl/model.html').then(function (response) {
                    me.tabs[0].tpl = response.data;
                });
            }

            if (is_first) {
                me.tabs[0].className = me.selected.tableName;
            }
            let data = {
                namespace: me.tabs[0].namespace || 'Model'
                , fields: me.tabs[0].fields.filter(item => me.tabs[0].selected_fields.indexOf(item.column_name) > -1)
                , table_name: me.selected.tableName
                , model_name: me.tabs[0].className
            }
            var render = template(me.tabs[0].tpl, data);
            me.tabs[0].content = render;
        }
        , async buildIDalFile(is_first) {
            let me = this;
            if (!me.tabs[1].tpl) {
                await axios.get('/lib/template/tpl/idal.html').then(function (response) {
                    me.tabs[1].tpl = response.data;
                });
            }
            if (is_first) {
                me.tabs[1].className = 'I' + me.transTableName() + 'Dal';
            }
            let data = {
                namespace: me.tabs[1].namespace
                , model_namespace: me.tabs[0].namespace
                , class_name: me.tabs[1].className
                , model_name: me.tabs[0].className
            }
            var render = template(me.tabs[1].tpl, data);
            me.tabs[1].content = render;
        }
        , async buildDalFile(is_first) {
            let me = this;
            if (!me.tabs[2].tpl) {
                await axios.get('/lib/template/tpl/dal.html').then(function (response) {
                    me.tabs[2].tpl = response.data;
                });
            }
            if (is_first) {
                me.tabs[2].className = me.transTableName() + 'Dal';
            }
            let data = {
                namespace: me.tabs[2].namespace
                , idal_namespace: me.tabs[1].namespace
                , model_namespace: me.tabs[0].namespace
                , idal_class_name: me.tabs[1].className
                , class_name: me.tabs[2].className
                , model_name: me.tabs[0].className
            }
            var render = template(me.tabs[2].tpl, data);
            me.tabs[2].content = render;
        }
        , async buildIBllFile(is_first) {
            let me = this;
            if (!me.tabs[3].tpl) {
                await axios.get('/lib/template/tpl/ibll.html').then(function (response) {
                    me.tabs[3].tpl = response.data;
                });
            }
            let keyInfo = me.getKeyAndKeyType();
            if (is_first) {
                me.tabs[3].className = 'I' + me.transTableName() + 'Bll';
            }
            let data = {
                namespace: me.tabs[3].namespace
                , fields: me.tabs[3].fields.filter(item => me.tabs[3].selected_fields.indexOf(item.column_name) > -1)
                , model_namespace: me.tabs[0].namespace
                , key: keyInfo[0]
                , key_type: keyInfo[1]
                , class_name: me.tabs[3].className
                , model_name: me.tabs[0].className
            }
            var render = template(me.tabs[3].tpl, data);
            me.tabs[3].content = render;
        }
        , async buildBllFile(is_first) {
            let me = this;
            if (!me.tabs[4].tpl) {
                await axios.get('/lib/template/tpl/bll.html').then(function (response) {
                    me.tabs[4].tpl = response.data;
                });
            }
            let keyInfo = me.getKeyAndKeyType();
            if (is_first) {
                me.tabs[4].className = me.transTableName() + 'Bll';
            }
            let data = {
                namespace: me.tabs[4].namespace
                , fields: me.tabs[3].fields.filter(item => me.tabs[3].selected_fields.indexOf(item.column_name) > -1)
                , dal_namespace: me.tabs[2].namespace
                , idal_namespace: me.tabs[1].namespace
                , ibll_namespace: me.tabs[3].namespace
                , model_namespace: me.tabs[0].namespace
                , key: keyInfo[0]
                , key_type: keyInfo[1]
                , class_name: me.tabs[4].className
                , idal_class_name: me.tabs[1].className
                , ibll_class_name: me.tabs[3].className
                , model_name: me.tabs[0].className
            }
            var render = template(me.tabs[4].tpl, data);
            me.tabs[4].content = render;
        }
        , async buildControllerFile(is_first) {
            let me = this;
            if (!me.tabs[5].tpl) {
                await axios.get('/lib/template/tpl/controller.html').then(function (response) {
                    me.tabs[5].tpl = response.data;
                });
            }
            let keyInfo = me.getKeyAndKeyType();
            if (is_first) {
                me.tabs[5].className = me.transTableName() + 'Controller';
            }
            let data = {
                namespace: me.tabs[5].namespace
                , fields: me.tabs[3].fields.filter(item => me.tabs[3].selected_fields.indexOf(item.column_name) > -1)
                , class_name: me.tabs[5].className
                , ibll_namespace: me.tabs[3].namespace
                , model_namespace: me.tabs[0].namespace
                , key: keyInfo[0]
                , key_type: keyInfo[1]
                , ibll_class_name: me.tabs[3].className
                , model_name: me.tabs[0].className
            }
            var render = template(me.tabs[5].tpl, data);
            me.tabs[5].content = render;
        }
        , async buildJsFile(is_first) {
            let me = this;
            if (!me.tabs[6].tpl) {
                await axios.get('/lib/template/tpl/js.html').then(function (response) {
                    me.tabs[6].tpl = response.data;
                });
            }
            if (is_first) {
                me.tabs[6].className = me.tabs[0].className.toLowerCase().replace(/^tbl_/, '').replace(/_/ig, '') + ".index.js";
            }
            let keyInfo = me.getKeyAndKeyType();
            let data = {
                fields: me.tabs[6].fields.filter(item => me.tabs[6].selected_fields.indexOf(item.column_name) > -1)
                , query_fields: me.tabs[3].fields.filter(item => me.tabs[3].selected_fields.indexOf(item.column_name) > -1)
                , model_name: me.tabs[0].className
                , controller_class_name: me.tabs[5].className
                , key: keyInfo[0]
            }
            var render = template(me.tabs[6].tpl, data);
            me.tabs[6].content = render;
        }
        , async buildViewFile(is_first) {
            let me = this;
            if (!me.tabs[7].tpl) {
                await axios.get('/lib/template/tpl/view.html').then(function (response) {
                    me.tabs[7].tpl = response.data;
                });
            }

            if (is_first) {
                me.tabs[7].className = "Index.cshtml";
                me.tabs[7].path = me.tabs[7].path + "\\" + me.tabs[5].className.replace('Controller', '');
            }
            let data = {
                fields: me.tabs[7].fields.filter(item => me.tabs[7].selected_fields.indexOf(item.column_name) > -1)
                , query_fields: me.tabs[3].fields.filter(item => me.tabs[3].selected_fields.indexOf(item.column_name) > -1)
                , model_name: me.tabs[0].className
            }
            var render = template(me.tabs[7].tpl, data);
            me.tabs[7].content = render;
        }
        , async saveFile(item) {
            let me = this;
            let loadingInstance = this.$loading({ fullscreen: true });
            axios.post('/CodeMode/SaveFile/', {
                path: item.path,
                content: encodeURIComponent(item.content),
                file_name: item.className.indexOf('.') == -1 ? (item.className + '.cs') : item.className
            }).then(function (response) {
                if (response.data && response.data.success) {
                    me.$message.success('保存成功');
                }
                else {
                    if (response.data) {
                        me.$message.error(response.data.message);
                    }
                    else {
                        me.$message.error('保存失败');
                    }
                }
                loadingInstance.close();
            }).catch(function (error) {
                me.$message.error(error);
                loadingInstance.close();
            })
        }
    }
});